//
//  YWLedgerEquListTableViewController.m
//  maintain
//
//  Created by 烟雨云渊 on 2017/4/20.
//  Copyright © 2017年 ewin. All rights reserved.
//

#import "YWLedgerEquListTableViewController.h"
#import "YWNoQrcodeEquipmentMaintainRecordDetailViewController.h"

#import "UIColor+CustomColors.h"
#import "MJRefresh.h"
#import "YWEquipmentManager.h"
#import "YWEquipment.h"
#import "YWCommonTool.h"
#import "YWMacro.h"
#import "YWBuildingManager.h"
#import "YWMissionManager.h"
#import "YWNoQrcodeLocationCellModel.h"
#import "YWBuilding.h"
#import "YWApartment.h"
#import "YWFloor.h"
#import "YWLocation.h"

#import "YWNoQrcodeLedgerEquipmentTableViewCell.h"

@interface YWLedgerEquListTableViewController ()
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) NSMutableArray * equTypeArray;
@end

@implementation YWLedgerEquListTableViewController

//- (instancetype)init{
//    return [super initWithStyle:UITableViewStyleGrouped];
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor customBackgroudColor];
    self.title = @"设备列表";
    [self setupTableView];
}

- (void)setupTableView{
    self.page = 1;
    [self.tableView registerClass:[YWNoQrcodeLedgerEquipmentTableViewCell class] forCellReuseIdentifier:YWNoQrcodeLedgerEquipmentTableViewCellIdentifier];
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.sectionFooterHeight = 0.01f;
    self.tableView.estimatedRowHeight = 44.0f;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshFootor)];
    self.tableView.mj_footer.automaticallyHidden = YES;
}

- (void)refreshFootor
{
    [self requestData:YWTableViewFreshTypeUp];
}

- (void)requestData:(YWTableViewFreshType)refreshType
{
    if (refreshType == YWTableViewFreshTypeUp) {
        self.page++;
    }
    YWWeakSelf(weakSelf);
    dispatch_async(dispatch_get_main_queue(), ^{
        [weakSelf.tableView.mj_footer endRefreshing];
        weakSelf.equTypeArray = nil;
        [weakSelf.tableView reloadData];
    });
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.equTypeArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.equTypeArray[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YWNoQrcodeLedgerEquipmentTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:YWNoQrcodeLedgerEquipmentTableViewCellIdentifier];
    YWEquipment *equipment = self.equTypeArray[indexPath.section][indexPath.row];
    cell.equipment = equipment;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    YWEquipment *equipment = self.equTypeArray[indexPath.section][indexPath.row];
    YWNoQrcodeEquipmentMaintainRecordDetailViewController * maintainRecondDetailVC = [[YWNoQrcodeEquipmentMaintainRecordDetailViewController alloc] init];
    NSArray* pictures = [[YWMissionManager shareManager] picturesWithEquipmentId:equipment.equipmentId pictureTroubleType:YWPictureTroubleTypeEquipment];
    equipment.pictures = pictures;
    maintainRecondDetailVC.equipment=equipment;
    maintainRecondDetailVC.isScanEnter = self.qrcodeId ? YES : NO;
    [self.navigationController pushViewController:maintainRecondDetailVC animated:YES];
}

- (UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
    static NSString* HeaderIdentifier = @"YWLedgerEquListTableViewControllerForHeaderInSection";
    
    UITableViewHeaderFooterView* headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HeaderIdentifier];
    if (headerView == nil) {
        headerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:HeaderIdentifier];
        headerView.contentView.backgroundColor = [UIColor customBackgroudColor];
        UILabel* label = [[UILabel alloc] init];
        label.textAlignment = NSTextAlignmentLeft;
        label.textColor = [UIColor customTextGrayColor];
        label.tag = 1009;
        label.numberOfLines = 0;
        label.font = KSmallContentFont;
        [headerView addSubview:label];
    }
    NSString *headText = [self tableViewHeadTxt:section];
    UILabel* label = [headerView viewWithTag:1009];
    label.text = headText;
    label.frame = CGRectMake(15, 0, SCREEN_WIDTH - 2 * 15, 30);
    return headerView;
}

-(NSString *)tableViewHeadTxt:(NSInteger)section{
   NSArray *array = [self.equTypeArray[section] count]>0?self.equTypeArray[section]:nil;
    YWEquipment * equipment = array.count?array[0]:nil;
    NSString * title = [[YWBuildingManager shareManager] areaLocationWithLocationId:equipment.locationId];
    return title;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}

- (NSMutableArray*)equTypeArray{
    if (!_equTypeArray) {
        _equTypeArray = [NSMutableArray array];
        [_equTypeArray addObjectsFromArray:[[YWEquipmentManager shareManager]equipmentArrayOrderByLocationIdWithBuildingId:self.locationModel.building.buildingId ApartmentId:self.locationModel.apartment.apartmentId floorId:self.locationModel.floor.floorId locationId:self.locationModel.location.locationId eqipmentSubSystemType:0 equipmentTypeId:self.equipmentTypeId page:self.page]];
    }
    return _equTypeArray;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
